package com.gk.util
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 *游戏Sprite
	 * 
	 * <br/>从舞台移除时，自动卸载自身所有侦听 
	 * @author Nestor
	 * 
	 */	
	public class GameSprite extends Sprite
	{
		private var listenerArr:Array;
		public function GameSprite(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number =  0)
		{
			move(xpos, ypos);
			addEventListener(Event.REMOVED_FROM_STAGE,removeFromStage);
			if(parent != null)
			{
				parent.addChild(this);
			}
		}
		
		public function draw(w:int,h:int,color:uint=0,alpha:Number=0):void{
			graphics.beginFill(color,alpha);
			graphics.drawRect(0,0,w,h);
			graphics.endFill();
		}
		
		public function move(xpos:Number, ypos:Number):void
		{
			x = Math.round(xpos);
			y = Math.round(ypos);
		}
		
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.addEventListener(type, listener, useCapture, priority, useWeakReference);
			var obj:Object = {};
			obj.type = type;
			obj.listener = listener;
			obj.useCapture = useCapture;
			obj.priority = priority;
			obj.useWeakReference = useWeakReference;
			
			if(!listenerArr)listenerArr=[];
			listenerArr.push(obj);
		}
		private function removeFromStage(e:Event):void
		{
			while(listenerArr.length>0){
				var obj:Object = listenerArr.pop();
				removeEventListener(obj.type, obj.listener, obj.useCapture);
			}
		}
		
		
		
	}
}